package servlet.shop;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.Shop;
import service.ShopService;

@WebServlet("/ShopOperationServlet")
public class ShopOperationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ShopOperationServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ShopService service = new ShopService();
		
		String operation = request.getParameter("operation");
		String sid = request.getParameter("sid");
		String name = request.getParameter("name");
		String tel = request.getParameter("tel");
		String address = request.getParameter("address");
		String status = request.getParameter("status");
		String describe = request.getParameter("describe");
		String id = request.getParameter("id");
		
		switch(operation) {
		case "del":
			if (id != null)
				service.delShop(id);
			break;
		case "alt": 
			if (id != null) {
				Shop shop = service.getShopById(id);
				request.setAttribute("shop", shop);
				request.setAttribute("operation", "alt");
				request.getRequestDispatcher("/admin/shopinfo.jsp").forward(request, response);
				return;
			}
			
			service.alterShop(sid,name,tel,address,status,describe);
			response.sendRedirect("ShopServlet");
			break;
		case "add": 
			if (name != null && !name.equals("")) {
				service.addShop(name,tel,address,status,describe);
				response.sendRedirect("ShopServlet");
			}else {
				request.setAttribute("operation", "add");
				request.getRequestDispatcher("/admin/shopinfo.jsp").forward(request, response);
			}
			break;
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
